﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WPF_DataGridFromJson_Demo.Models;
using WPF_DataGridFromJson_Demo.ViewModels;

namespace WPF_DataGridFromJson_Demo.Views
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 编辑非法数据还原 暂时还无法理解
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            if (e.EditAction == DataGridEditAction.Commit)
            {
                var element = e.EditingElement as FrameworkElement;

                if (element != null)
                {
                    var bindingExpression = element.GetBindingExpression(TextBox.TextProperty);
                    if (bindingExpression != null)
                    {
                        var propertyName = bindingExpression.ParentBinding.Path.Path;
                        var textBox = element as TextBox;
                        if (textBox != null)
                        {
                            var item = e.Row.Item as RotorSegmentParameter;

                            try
                            {
                                if (double.TryParse(textBox.Text, NumberStyles.Any, CultureInfo.InvariantCulture, out double newValue))
                                {
                                    // 获取 DataGrid 的 DataContext，通常是 ObservableCollection
                                    var dataGrid = sender as DataGrid;
                                    var items = dataGrid.ItemsSource as ObservableCollection<RotorSegmentParameter>;

                                    if (items != null)
                                    {
                                        // 确保新的值在合法范围内
                                        if (newValue < 0 || newValue >= 360)
                                        {
                                            RestoreOriginalValue(item, propertyName, e);
                                        }
                                        else
                                        {
                                            e.Row.Item.GetType().GetProperty(propertyName).SetValue(e.Row.Item, newValue);
                                        }
                                    }
                                    else
                                    {
                                        RestoreOriginalValue(item, propertyName, e);
                                    }
                                }
                                else
                                {
                                    RestoreOriginalValue(item, propertyName, e);
                                }
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine($"Exception: {ex.Message}");
                                RestoreOriginalValue(item, propertyName, e);
                            }
                            // 确保界面上显示最新的值
                            bindingExpression.UpdateTarget();
                        }
                    }
                }
            }
        }
        private void RestoreOriginalValue(RotorSegmentParameter item, string propertyName, DataGridCellEditEndingEventArgs e)
        {
            var originalValue = e.Row.Item.GetType().GetProperty(propertyName).GetValue(e.Row.Item);
            e.Row.Item.GetType().GetProperty(propertyName).SetValue(item, originalValue);
        }

        private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                // 执行确认逻辑，例如保存数据或执行其他操作

                // 让 TextBox 失去焦点
                var textBox = sender as TextBox;
                if (textBox != null)
                {
                    textBox.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
                }

                e.Handled = true; // 防止回车键继续传递
            }
        }

    }
}
